'''
Description: 
Author: Zilu
Date: 2021-11-04 13:46:16
Version: 
LastEditTime: 2021-11-04 14:05:02
LastEditors: Zilu
'''

# 使用m:n的形式，获取切片
L = ['Michael','Tom','Tracy','Alice','Sarah']
b = L[0:3]
r = L[-2:]

nums = list(range(100))
two = nums[:10:2]
five = nums[::5]

# 字符串和tuple都可以像tuple一样切片

# 练习：去除字符串首尾的空格
def trim(s):
    if (len(s)==0):
        return ''
    first = 0
    last = len(s) - 1
    while (s[first]==' '):
        first+=1
        if (first >= len(s)):
            return ''
    while(s[last]==' '):
        last-=1
    if (first>=last):
        return ''
    else:
        return s[first:last+1]

if trim('hello  ') != 'hello':
    print('测试失败!')
elif trim('  hello') != 'hello':
    print('测试失败!')
elif trim('  hello  ') != 'hello':
    print('测试失败!')
elif trim('  hello  world  ') != 'hello  world':
    print('测试失败!')
elif trim('') != '':
    print('测试失败!')
elif trim('    ') != '':
    print('测试失败!')
else:
    print('测试成功!')

# 可以用下递归

